package top.healthylife.gateway.mgr.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.healthylife.basics.utils.exception.BusinessException;
import top.healthylife.basics.utils.utils.R;
import top.healthylife.gateway.mgr.service.GatewayLogService;
import top.heathylife.gateway.common.po.GatewayLogByPage;
import top.heathylife.gateway.common.po.GatewayLogPo;

import java.time.LocalDateTime;

/**
 * @author maoxiaomeng
 */
@Api(tags = "数据网关服务-接口日志信息")
@Slf4j
@RestController
@RequestMapping("/gateway/interface/log")
@AllArgsConstructor
public class InterfaceLogController {

    GatewayLogService gatewayLogService;


    /**
     * 查询数据网关日志
     * 查询条件
     * <ul>
     *     <li>id查询</li>
     *     <li>接口名称查询(=)</li>
     *     <li>数据源简称(=)</li>
     *     <li>调用方名称(=)</li>
     *     <li>开始时间-结束时间 区间查询</li>
     *     <li>是否查看缓存(=)</li>
     *     <li>秘钥查询(=)</li>
     *     <li>入参查询(like)</li>
     *     <li>流水号查询(=)</li>
     * </ul>
     *
     * @return 分页查询对象
     */
    @ApiOperation("日志分页查询")
    @PostMapping("/page")
    R<Page<GatewayLogPo>> getGatewayLogByPage(@RequestBody GatewayLogByPage gatewayLogByPage) {
        GatewayLogPo gatewayLogPo = gatewayLogByPage.getGatewayLogPo();
        Page<GatewayLogPo> page = gatewayLogByPage.getPage();
        LambdaQueryWrapper<GatewayLogPo> gatewayLogPoLambdaQueryWrapper = Wrappers.lambdaQuery();
        LocalDateTime startTime = gatewayLogPo.getCreateTime();
        LocalDateTime endTime = gatewayLogPo.getFinishTime();
        gatewayLogPoLambdaQueryWrapper
                .eq(StrUtil.isNotBlank(gatewayLogPo.getId()), GatewayLogPo::getId, gatewayLogPo.getId())
                .like(StrUtil.isNotBlank(gatewayLogPo.getApiName()), GatewayLogPo::getApiName, gatewayLogPo.getApiName())
                .eq(StrUtil.isNotBlank(gatewayLogPo.getShortName()), GatewayLogPo::getShortName, gatewayLogPo.getShortName())
                .eq(StrUtil.isNotBlank(gatewayLogPo.getApplicationName()),
                        GatewayLogPo::getApplicationName, gatewayLogPo.getApplicationName())
                .between(ObjectUtil.isAllNotEmpty(startTime, endTime), GatewayLogPo::getCreateTime, startTime, endTime)
                .eq(ObjectUtil.isNotEmpty(gatewayLogPo.getHitCache()), GatewayLogPo::getHitCache, gatewayLogPo.getHitCache())
                .eq(ObjectUtil.isNotEmpty(gatewayLogPo.getHitCache()), GatewayLogPo::getHitCache, gatewayLogPo.getHitCache())
                .eq(ObjectUtil.isNotEmpty(gatewayLogPo.getSecret()), GatewayLogPo::getSecret, gatewayLogPo.getSecret())
                .like(ObjectUtil.isNotEmpty(gatewayLogPo.getRequestData()), GatewayLogPo::getRequestData, gatewayLogPo.getRequestData())
                .eq(ObjectUtil.isNotEmpty(gatewayLogPo.getNo()), GatewayLogPo::getNo, gatewayLogPo.getNo())
                .orderByDesc(GatewayLogPo::getCreateTime);
        try {
            return R.ok(gatewayLogService.page(page, gatewayLogPoLambdaQueryWrapper));
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException("日志查询异常");
        }
    }


}
